package com.net.xpay.admin.controller.admin;

import com.net.common.exception.BusinessException;
import com.net.common.model.ResultData;
import com.net.common.model.ResultPagedList;
import com.net.common.util.MoneyUtil;
import com.net.xpay.admin.vo.SumUserAccountMoneyLeftStatVo;
import com.net.xpay.admin.vo.UserAccountMoneyLeftStatVo;
import com.net.xpay.common.domain.stat.UserAccountMoneyLeftStat;
import com.net.xpay.common.manager.stat.UserAccountMoneyLeftStatManager;
import com.net.xpay.common.model.search.UserAccountMoneyLeftStatSearchModel;
import com.net.xpay.core.constant.PoseidonErrorCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 *

 * 公司账单日常统计
 */
@RestController
@RequestMapping("/admin/user-account-money-left-stat")
@Api(tags = "运营/商户服务费余额日留存统计", description = "运营 公司账单日常统计 net")
public class AdminUserAccountMoneyLeftController {

    @Autowired
    private UserAccountMoneyLeftStatManager userAccountMoneyLeftStatManager;

    @ApiOperation("分页搜索")
    @PostMapping("/search")
    @ResponseBody
    @PreAuthorize("hasAnyRole('ADMIN', 'user-account-money-left-stat::search')")
    public ResultPagedList<UserAccountMoneyLeftStatVo> searchUserAccountMoneyLeftStat(@RequestBody UserAccountMoneyLeftStatSearchModel searchModel) {
        if (searchModel.getDay() == null) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "请选择截止日期");
        }
        List<UserAccountMoneyLeftStat> records = userAccountMoneyLeftStatManager.search(searchModel);
        List<UserAccountMoneyLeftStatVo> vos = new ArrayList<>();
        for (UserAccountMoneyLeftStat stat : records) {
            UserAccountMoneyLeftStatVo vo = new UserAccountMoneyLeftStatVo();
            vo.setUserId(stat.getUserId());
            vo.setDay(stat.getDay());
            vo.setBeforeMoneyLeft(MoneyUtil.convertCentToString(stat.getBeforeMoneyLeft(), 2));
            vo.setChangeMoney(MoneyUtil.convertCentToString(stat.getChangeMoney(), 2));
            vo.setMoneyLeft(MoneyUtil.convertCentToString(stat.getMoneyLeft(), 2));
            vo.setId(stat.getId());
            vo.setCreateTime(stat.getCreateTime());
            vo.setModifyTime(stat.getModifyTime());
            vos.add(vo);
        }
        long count = userAccountMoneyLeftStatManager.searchCount(searchModel);
        return new ResultPagedList<>(vos, count, searchModel);
    }


    @ApiOperation("聚合用户服务费余额日留存")
    @PostMapping("/sum")
    @ResponseBody
    @PreAuthorize("hasAnyRole('ADMIN', 'user-account-money-left-stat::search')")
    public ResultData<SumUserAccountMoneyLeftStatVo> sumUserAccountMoneyLeftStat(@RequestBody UserAccountMoneyLeftStatSearchModel searchModel) {
        if (searchModel.getDay() == null) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "请选择截止日期");
        }
        UserAccountMoneyLeftStat stat = userAccountMoneyLeftStatManager.sumUserAccountMoneyLeftStat(searchModel);
        SumUserAccountMoneyLeftStatVo vo = new SumUserAccountMoneyLeftStatVo();
        vo.setBeforeMoneyLeft(MoneyUtil.convertCentToString(stat.getBeforeMoneyLeft(), 2));
        vo.setChangeMoney(MoneyUtil.convertCentToString(stat.getChangeMoney(), 2));
        vo.setMoneyLeft(MoneyUtil.convertCentToString(stat.getMoneyLeft(), 2));
        return new ResultData<>(vo);
    }


}
